const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const handleDate = function (format, value = '') {
  if (!value) return '--'
  let [_type, _date] = [format, new Date(value)]
  return _type
    .replace(/(y{4})/, _date.getFullYear())
    .replace(/(M{2})/, () => _date.getMonth() < 9 ? '0' + (_date.getMonth() + 1) : (_date.getMonth() + 1).toString())
    .replace(/d{2}/, () => _date.getDate() < 10 ? '0' + (_date.getDate()) : _date.getDate().toString())
    .replace(/H{2}/, () => _date.getHours() < 10 ? '0' + (_date.getHours()) : _date.getHours().toString())
    .replace(/m{2}/, () => _date.getMinutes() < 10 ? '0' + (_date.getMinutes()) : _date.getMinutes().toString())
    .replace(/s{2}/, () => _date.getSeconds() < 10 ? '0' + (_date.getSeconds()) : _date.getSeconds().toString())

}

module.exports = {
  formatTime: formatTime,
  handleDate: handleDate
}
